package logic

import (
	"context"
	"cp/cprpc/cprpc"
	"cp/cprpc/internal/svc"
	"cp/models/mysql"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type EvaluationAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewEvaluationAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EvaluationAddLogic {
	return &EvaluationAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *EvaluationAddLogic) EvaluationAdd(in *cprpc.EvaluationAddRequest) (*cprpc.EvaluationAddResponse, error) {
	// todo: add your logic here and delete this line
	type findEvaluation mysql.Evaluation
	mysql.DB.Debug().Where("content=?", in.Content).Find(&findEvaluation{})
	if in.Content == "欺诈,劣质" {
		return nil, errors.New("含违规词汇,请修改")
	}

	eval := mysql.Evaluation{
		CourseId:    int(in.CourseId),
		UserId:      int(in.UserId),
		Score:       in.Score,
		Content:     in.Content,
		IsAnonymous: in.IsAnonymous,
		Status:      in.Status,
	}
	mysql.DB.Debug().Create(&eval)

	return &cprpc.EvaluationAddResponse{
		Evaluation: int64(eval.ID),
	}, nil
}
